#include "compiler.hpp"
#include "runner.hpp"
#include <cstdio>
#include "comm/httplib.h"
#include "compile_run.hpp"
using namespace ns_cpmpiler;
using namespace ns_runner;
using namespace httplib;
// 编译服务谁是都有可能被多个人请求

void Usage(const std::string& proc)
{
    std::cerr << "Usage:" << "\n\t" << proc << "";
}
int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }
    Server svr;
    svr.Get("/hello", [](const Request& req, Response& resp){
        resp.set_content("hello httplib， 你好 httplib","text/pain; charset=utf-8");
    });

    svr.Post("/compile_and_run", [](const Request& req, Response& resp){
        // 用户请求的服务正文是我们想要的 json string
        std::string in_json = req.body, out_json;
        if (!in_json.empty())
        {
            CompilerAndRun::Start(in_json, &out_json);
            resp.set_content(out_json, "application/json;charset=utf-8");
        }
    });
    svr.listen("0.0.0.0", std::stoi(argv[1]));



    // std::string in_string, out_string;
    // std::ifstream out(PathUtil::Src("code"));
    // std::string line;
    // while (std::getline(out, line))
    // {
    //     in_string += line;
    //     in_string += "\n";
    // }
    // // std::cout << in_string << std::endl;
    // std::string in_json;
    // Json::Value in_value;
    // in_value["code"] = in_string;
    // in_value["input"] = "";
    // in_value["cpu_limit"] = 1;
    // in_value["mem_limit"] = 1024 * 256;
    // Json::FastWriter writer;
    // in_json = writer.write(in_value);
    // CompilerAndRun::Start(in_json, &out_string);
    // std::cout << out_string << std::endl;
    // return 0;

}
